﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;
using System.Collections;
using System.Configuration;

namespace ExpertMIS.Admin.UserControl
{
    public partial class TalentForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #endregion

        #region 公有方法

        public TalentEntity Entity
        {
            get
            {
                return ViewState["_TalentEntity_"] as TalentEntity;
            }
            set
            {
                ViewState["_TalentEntity_"] = value;
            }
        }

        public void LoadTalent(string TalentOID)
        {
            TalentBroker broker = new TalentBroker();

            Hashtable htQuery = new Hashtable();
            htQuery.Add("OID",TalentOID);
            Entity = broker.GetEntity(htQuery) as TalentEntity;

            if (Entity == null)
            {
                Entity = broker.NewEntity() as TalentEntity;
            }

            txDescribe.Text = Entity.Summary;
            txSort.Text = Entity.SortNo.ToString();

            // Modify, wdong, 2009-9-3, add default nopic
            if (string.IsNullOrEmpty(Entity.ImagePath))
            {
                Entity.ImagePath = "";
            }

            imgPhoto.ImageUrl = "~/Service/GetPhoto.aspx?w=128&h=128&filename=" + Entity.ImagePath;

        }

        public bool SaveTalent()
        {
            try
            {
                TalentBroker broker = new TalentBroker();

                Entity.Summary = txDescribe.Text;
                int sortNo = 0;
                int.TryParse(txSort.Text,out sortNo);
                Entity.SortNo = sortNo;

                if (fuImage.HasFile)
                {
                    string filename = Guid.NewGuid().ToString() + "." + System.IO.Path.GetExtension(fuImage.FileName);
                    string filepath = Server.MapPath(ConfigurationManager.AppSettings["UserUpload"]);
                    fuImage.SaveAs(filepath + filename);
                    Entity.ImagePath = filename;
                }

                broker.SaveEntity(Entity, null);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion
    }
}